package com.ruoyi.api.kehu;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.JiajiaAppVersion;
import com.ruoyi.system.service.IJiajiaAppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/api/kehu/version")
@Api(tags = "用户端版本")
public class ApiAppUserVersionController {

    @Autowired
    private IJiajiaAppVersionService versionService;

    @ApiOperation("获取最新版本")
    @PostMapping("/getVersion")
    public AjaxResult getVersion()
    {
        JSONObject jsonObject = new JSONObject();

        PageHelper.orderBy("versionCode desc");
        JiajiaAppVersion appVersion = new JiajiaAppVersion();
        appVersion.setPlatform(TokenUtil.getPlatForm());
        appVersion.setType(0);
        List<JiajiaAppVersion> appVersionList = versionService.selectJiajiaAppVersionList(appVersion);
        if (!appVersionList.isEmpty()){
            appVersion = appVersionList.get(0);
            if (TokenUtil.getPlatForm().equals(TokenUtil.platform_android)){
                appVersion.setPath(appVersion.getPath());
            }
            appVersion.setContent(appVersion.getContent().replace("\\n","\n"));
            jsonObject.put("appVersion",appVersion);
        }else {
            jsonObject.put("appVersion",new JSONObject());
        }
        return AjaxResult.success(jsonObject);
    }
}
